package com.samuel.blog.service.impl;

import com.samuel.blog.dao.TagDao;
import com.samuel.blog.entity.TagEntity;
import com.samuel.blog.service.TagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 *      标签服务层
 * </p>
 *
 * @author kaiji
 * @since 2019/11/17 20:50
 */
@Service("tagService")
public class TagServiceImpl extends BaseServiceImpl<TagEntity, String > implements TagService  {

    @Resource(name = "tagDao")
    private TagDao tagDao;


    @Override
    @Transactional(readOnly = true)
    public TagEntity selectByName(String name) {
        Map<String, Object> filterMap = new HashMap<>(16);
        filterMap.put("name", name);
        List<TagEntity> tagEntities = tagDao.selectByMap(filterMap);
        return null == tagEntities || tagEntities.size() <= 0 ? null : tagEntities.get(0);
    }

    @Override
    @Transactional(readOnly = true)
    public List<TagEntity> selectByArticleId(String articleId) {
        List<TagEntity> list = tagDao.selectByArticleId(articleId);
        return Optional.ofNullable(list).orElse(new ArrayList<>());
    }

    @Override
    public int deleteAndArticle(String tagId, String articleId) {
        Map<String, Object> filterMap = new HashMap<>();
        filterMap.put("tagId", tagId);
        filterMap.put("articleId", articleId);
        return tagDao.deleteAndArticle(filterMap);
    }
}
